<?php

	global $HTTP_GET_VARS;
	global $HTTP_POST_VARS;
	global $account;
	global $account_state;
	global $error;
	global $sql_row, $sql, $sql_total;
	
	$game = $_GET['game'];
	$progress = $_GET['progress'];

	if ( $account_state == 0)
	{
		error_redirect('Admin-Site is Admin-Only, please validate yourself as Admin-Guy.',ERROR_LOGIN_REQUIRED,'../distribution/?action=showusers');
	}
	if ($account['admin'] != 1)
	{
		error_redirect('Admin-Site is Admin-Only, your was insuccessfully validated as Admin-Guy.',ERROR_ILLEGAL_REQUEST);
	}
	if ( $account_state == 1 && $account['admin'] == 1)
	{

		if (isset($game))
		{
			if (file_exists('scripts/'.$game.'.php'))
			{
				include_once('scripts/'.$game.'.php');
			}
			if (isset($progress))
			{
				output('<p><b class="text_big">Reached map '.$progress_id_to_value[$game][$progress].' in  <a href="?action=showgame&amp;game='.$game.'">'.$game.'</a></b><br />');			
				output('This list only includes those who have reached map '.$progress_id_to_value[$game][$progress].' in <a href="?action=showgame&amp;game='.$game.'">'.$game.'</a>.<br />' );			
				output('You can also view a list of <a href="?action=showusers&amp;game='.$game.'">every user who has installed '.$game.'</a>.<br />');
				output('You can also view a list of <a href="?action=showusers">every user who has installed products from Maxsi Distribution</a>.<br /><br />');
						
				$register_slot = sql("SELECT * FROM distribution_register WHERE product = '".$game."' AND progress >= '".$progress."' ORDER by product DESC, installed DESC, id DESC");
			}
			else
			{
				output('<p><b class="text_big">Users who have installed <a href="?action=showgame&amp;game='.$game.'">'.$game.'</a></b><br />');			
				output('This list only includes those who have installed <a href="?action=showgame&amp;game='.$game.'">'.$game.'</a>.<br />' );			
				output('You can also view a list of <a href="?action=showusers">every user who has installed products from Maxsi Distribution</a>.<br /><br />');
						
				$register_slot = sql("SELECT * FROM distribution_register WHERE product = '".$game."' ORDER by product DESC, installed DESC, id DESC");
			}
		}
		else
		{
			output('<p><b class="text_big">Users who have installed products from Maxsi Distribution</b><br />');						
			output('This list includes every user who has installed products from Maxsi Distribution. If you wish to view stats for a specific game, simply go to the <a href="?action=showgames">list of games</a>, and find a specific game.<br /><br />' );
			
			$register_slot = sql("SELECT * FROM distribution_register ORDER by product DESC, installed DESC, id ASC");
		}
		
		
		if ( $sql_total[$register_slot] == 0 )
		{
			output('None has come so far!');
		}
		else
		{
			$counter=0;
			$lastproduct = '';
			do
			{
				if (strcmp($lastproduct,$sql_row[$register_slot]['product'])!=0)
				{
					$idcounter = 1;
					$lastproduct = $sql_row[$register_slot]['product'];
					if (file_exists('scripts/'.$sql_row[$register_slot]['product'].'.php'))
					{
						include_once('scripts/'.$sql_row[$register_slot]['product'].'.php');
					}
				}
				else
				{
					$idcounter+=1;
				}
	
				if (isset($progress_id_to_value[$sql_row[$register_slot]['product']]))
				{
					$sql_row[$register_slot]['progress'] = $progress_id_to_value[$sql_row[$register_slot]['product']][$sql_row[$register_slot]['progress']];
				}
				output( $sql_row[$register_slot]['product'] . ' ' . $sql_row[$register_slot]['version'] . ' ' . $idcounter . ' ' . $sql_row[$register_slot]['installed'] . ' <a href="?action=showuser&amp;account='.$sql_row[$register_slot]['username'].'">' . $sql_row[$register_slot]['username'] . '</a> '. $sql_row[$register_slot]['progress'].'<br />
				');
				$counter+=1;
				
				sql_next($register_slot);
			}
			while ($counter<$sql_total[$register_slot]);
		}
		
		clearslot($register_slot);
		
		output('</p>');
	}
	else
	{
		error_redirect('ASSERTION FAILURE: $account_state must be either 0 or 1 and $account[\'admin\'] must be 1 or any other value',ERROR_SERVER_ERROR);
	}
?>